<?php
/**   Router:
      - identify the language of request
      - identify the module (including finding the default module) which will handle request
      - identify operation name (using Nodes or directly from path)
      - initialize controller by module and operation name
*     The extensions may: identify areas, rewrite module names, redirect depending on hostname/GeoIP/etc
*
*   All exceptions thrown by router must lead to a 404 page or when required, redirect to login
*
  CHANGELOG

*/
abstract class Yz_Router_Abstract
    extends Yz_Class
    implements Yz_Router_Interface
{
    /**
     *  @var Yz_Array
     */
    protected  $arguments;
    /**
     *  @var Yz_Exception
     */
    protected  $exception;
    /**
     *  @var Yz_HTTP_Request_Interface
     */
    protected  $request;
    /**
     *  @var Yz_HTTP_Response_Interface
     */
    protected  $response;
    /**
     *  @var bool
     */
    protected  $isPOST;

    /** The prefix to the dynamic pages generated by the YZ engine
     *      (in case no Rewrite used, it may be /index.php/ or /some-dir/index.php/ )
     *  @var string READONLY with trailing /
     */
    protected $webPrefix = "/";

    /** The web-based name of the YZ engine installation directory
     *  - probably relative to document-root, but it can be rewritten
     *  - all YZ items are relative to this dir, including styles, JS and images
     *  @var string READONLY with trailing /
     */
    protected $webRoot = "/"; // READONLY with trailing /



    /********************************
     *      Public methods          *
     *******************************/


    /** get the list of arguments
     *  @return Yz_Array
     */
    public function getArguments()
    {
        return $this->arguments;
    }

    /** get the last thrown Exception
     *
     */
    public function getException()
    {
        return $this->exception;
    }

    /**
     *  @return Yz_HTTP_Request_Interface
     */
    public function getRequest()
    {
        $rq =& $this->request;
        if (null === $rq) {
            $rq = \Yz_HTTP_Request::getInstance();
        }
        return $rq;
    }

    /**
     *  @return Yz_HTTP_Response_Interface
     */
    public function getResponse()
    {
        $rs =& $this->response;
        if (null === $rs) {
            $rs = \Yz_HTTP_Response::getInstance();
        }
        return $rs;
    }


    /** get the web-prefix for the dynamically generated pages
     *      e.g. /yz-install-dir/
     *      or   /yz-install-dir/index.php (if mod_rewrite not detected)
     *  @return string always contains leading and trailing /
     */
    public function getWebPrefix()
    {
        return $this->webPrefix;
    }

    /** get the web-root for all the engine's paths
     *      e.g. /                  (if installed in DOCUMENT_ROOT)
     *      or   /yz-install-dir/
     *  @return string always contains leading and trailing /
     */
    public function getWebRoot()
    {
        return $this->webRoot;
    }

    /** test if the route method is POST
     *  @return bool
     */
    public function isPOST()
    {
        return $this->isPOST;
    }


    /** set method to GET
     *  @return chainable
     */
    public function useGET()
    {
        $this->isPOST = false;
        return $this;
    }

    /** set method to POST
     *  @return chainable
     *
     */
    public function usePOST()
    {
        $this->isPOST = true;
        return $this;
    }

} // class

